#!/bin/bash
set -e

checkit() {
	#checkit <sha256sum> <filename>
	echo "$1  $2" | sha256sum -c
}

if [ "$#" == 0 ]
then
	foss=(*/)
else
	foss=($@)
fi

for i in "${foss[@]}"
do
	cd "$i"
	if [ -x download ]; then
		./download
	else
		source ./info
		fn="$(basename "$url")"
		if ! test -f "$fn"; then
			rm -f *.exe *.msi
		fi
		if ! checkit "$sha256sum" "$fn"; then
			wget -c "$url"
			if ! checkit "$sha256sum" "$fn"; then
				rm -f "$fn"
				wget "$url"
				checkit "$sha256sum" "$fn"
			fi
		fi
	fi
	cd ..
done
